package com.appspot.jcmswiki.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.appspot.jcmswiki.command.Command;
import com.appspot.jcmswiki.command.impl.Index;
import com.appspot.jcmswiki.command.impl.Install;
import com.appspot.jcmswiki.command.impl.Page;

public class RequestHelper {

	private static final String INDEX = "index";
	private static final String PAGE_COMMAND = "page";
	private static final String INSTALL_COMMAND = "install";
	private final Map<String, Command> commandMap;

	public RequestHelper() {
		commandMap = new HashMap<String, Command>();
		commandMap.put(INSTALL_COMMAND, new Install());
		commandMap.put(PAGE_COMMAND, new Page());
		commandMap.put(INDEX, new Index());
	}

	public Command getCommand(HttpServletRequest request) {
		System.out.println(request.getRequestURI());
		String[] arr = request.getRequestURI().split("/");
		Command command = commandMap.get(arr[1].toLowerCase());
		if (command != null){
			return command;
		} else {
			return commandMap.get(INDEX);
		}
	}
}
